﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


/*
 * 测试用例:
 * 1) dataCount=3
 *   a) 上下滑动没问题
 *   b) dataCount设为0, 正常
 *   c) dataCount设为20, 正常生成
 * 2) dataCount=0
 * 3) dataCount=20
 *   a) 往上滑动, 顶部回收, 底部生成
 *   b) 往下滑动, 顶部生成, 底部回收
 *   c) 从dataIndex=0开始初始化, 然后点击按钮, 快速往后滚动到dataIndex=8处
 *   d) 从dataIndex=10开始初始化, 然后点击按钮, 快速往前滚动到dataIndex=2处
 *   e) 从dataIndex=19开始初始化
 *   e) 从dataIndex=21开始初始化
 *  4) 条目不等高
 */

public class ListViewDemo : MonoBehaviour
{

    public ListView m_ListView;
    public RectTransform m_ItemTemplate;

    public Button m_Button;

    private void OnClick_Button()
    {
        m_ListView.ScrollToDataIndex(8, 0);
    }
    
    void Awake()
    {
        m_Button.onClick.AddListener(OnClick_Button);
    }
    
    // Start is called before the first frame update
    void Start()
    {
        m_ListView.SetItemDelegate(OnObtainItem, null);
        m_ListView.DataCount = 3;
        m_ListView.RefreshItems(0);
    }

    private RectTransform OnObtainItem(ListView listview, int index)
    {
        var rtf = GameObject.Instantiate<RectTransform>(this.m_ItemTemplate);
        rtf.name = "item_" + index;
        rtf.GetComponentInChildren<Text>().text = index.ToString();
        var sizeDelta = rtf.sizeDelta;
        //sizeDelta.y = 100 + index * 5;
        rtf.sizeDelta = sizeDelta;
        
        return rtf;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
